//
//  HMVistorView.swift
//  Weibo-swift
//
//  Created by GD on 16/9/20.
//  Copyright © 2016年 geduo. All rights reserved.
//

import UIKit

protocol HMVisitorDelegate:NSObjectProtocol {
    func loginBtnClick()
}

class HMVistorView: UIView {
    
    //闭包 或者 代理
    var loginClosure : (()->())?
    
    weak var delegate : HMVisitorDelegate?
    
    // 懒加载  旋转视图
    private lazy var cycyleView : UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
    //遮盖
     private lazy var maskImageView : UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_mask_smallicon"))
    //房子
    private lazy var iconImageView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //未登录信息视图  label
    // 提示 label
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        //textColor: UIColor.darkGray, fontSize: 14)
        label.textColor = UIColor.darkGray
        label.font = UIFont.systemFont(ofSize: 14)
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.numberOfLines = 0
        // 文本对齐方式
        label.textAlignment = .center
        return label
    }()
    
    //登录按钮
    private lazy var loginButton : UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        button.setTitle("登录", for: .normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        //  去掉点击的高亮效果
        button.adjustsImageWhenHighlighted = false
        return button
    }()
    
    // 注册按钮
    lazy var registerButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        button.setTitle("注册", for: .normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        //  去掉点击的高亮效果
        button.adjustsImageWhenHighlighted = false
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    private func setupUI(){
        addSubview(cycyleView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        
        cycyleView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(cycyleView)
        }
        
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(cycyleView)
        }
        
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(cycyleView)
            make.top.equalTo(cycyleView.snp_bottom)
            make.width.equalTo(224)
        }
        
        loginButton.snp_makeConstraints { (make) in
            make.left.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        
        registerButton.snp_makeConstraints { (make) in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(loginButton)
            make.size.equalTo(loginButton)
        }
    }
    
    
    func updateVisitorInfo(message:String?, imageName: String?) {
        if let msg = message,let imgName = imageName {//非首页
            messageLabel.text = msg
            iconImageView.image = UIImage(named: imgName)
            cycyleView.isHidden = true
        }else{//首页
            //旋转动画
            startAinmation()
        }
    }
    
    //开启动画
    private func startAinmation(){
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        //
        animation.isRemovedOnCompletion = false
        cycyleView.layer.add(animation, forKey: nil)
        
    }
    
    @objc private func loginAction(){
        print("我是访客视图中的登录点击")
        
        loginClosure?()
        
        delegate?.loginBtnClick()
    }
}
